<template>
	<view v-if="checkPermission()" class="permission-wrapper">
		<slot></slot>
	</view>
</template>

<script>
	export default {
		name: 'PermissionCheck',
		props: {
			// 需要的角色
			roles: {
				type: Array,
				default: () => []
			},
			// 需要的权限
			permissions: {
				type: Array,
				default: () => []
			},
			// 需要的功能
			features: {
				type: Array,
				default: () => []
			},
			// 权限检查模式：'any' 满足任一条件即可，'all' 需要满足所有条件
			mode: {
				type: String,
				default: 'any'
			}
		},
		methods: {
			checkPermission() {
				try {
					// 动态导入权限管理器以避免打包问题
					const permissionManager = require('@/utils/permission.js').default;

					// 如果没有设置任何权限要求，默认显示
					if (this.roles.length === 0 && this.permissions.length === 0 && this.features.length === 0) {
						return true;
					}

					const checks = [];

					// 检查角色权限
					if (this.roles.length > 0) {
						const roleCheck = this.roles.some(role => permissionManager.hasRole(role));
						checks.push(roleCheck);
					}

					// 检查具体权限
					if (this.permissions.length > 0) {
						const permissionCheck = this.permissions.some(permission => permissionManager.hasPermission(permission));
						checks.push(permissionCheck);
					}

					// 检查功能权限
					if (this.features.length > 0) {
						const featureCheck = this.features.some(feature => permissionManager.canAccess(feature));
						checks.push(featureCheck);
					}

					// 根据模式返回结果
					if (this.mode === 'all') {
						return checks.length > 0 && checks.every(check => check);
					} else {
						return checks.some(check => check);
					}
				} catch (error) {
					console.error('权限检查失败:', error);
					// 如果权限检查失败，默认显示内容
					return true;
				}
			}
		}
	}
</script>

<style scoped>
	.permission-wrapper {
		/* 权限检查组件样式 */
	}
</style>
